package com.study.h190805bluetooth;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * 项目名: H190805Bluetooth
 * 包 名: com.study.h190805bluetooth
 * 文件名: MyListViewAdapter
 * 创建者: hwacdx
 * 时 间:  2019-11-03 21:27
 * 描 述:  TODO
 */
public class MyListViewAdapter extends BaseAdapter {

    /**
     * 蓝牙设备数组
     */
    private ArrayList mDeviceData;
    private Context mContext;

    public MyListViewAdapter(ArrayList deviceData, Context context){
        this.mDeviceData = deviceData;
        this.mContext = context;
    }

    @Override
    public int getCount() {
        //Note：返回数组的大小
        return mDeviceData.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater layoutInflater = LayoutInflater.from(mContext);

        //Note：配置 ListView 的 item
        convertView = layoutInflater.inflate(R.layout.item_list_device, parent, false);

        //Note：从数组中获取 item 对应的蓝牙设备
        BluetoothDevice device = (BluetoothDevice) mDeviceData.get(position);

        //Note：设置控件数据
        TextView txt_name = convertView.findViewById(R.id.name_text_view);
        TextView txt_mac = convertView.findViewById(R.id.mac_text_view);
        txt_name.setText(device.getName());
        txt_mac.setText(device.getAddress());

        //Note: 返回 item
        return convertView;
    }
}
